# test2
from nicegui import ui
from ex4nicegui import bi
import pandas as pd
import time

# 利用bi.data_source类来绑定dataframe数据源,实现实时更新表格
# 初始数据
data = pd.DataFrame(
    {"name": ["f", "a", "c", "b", "f", "a", "c", "b"], "age": [1, 2, 3, 1, 1, 2, 3, 1]}
)
ds = bi.data_source(data)
ds.ui_table()
time.sleep(5)
# data = pd.DataFrame({"name": ["new1", "new2", "new3"], "age": [20, 21, 22]})
# ds.reload(data)


# 定义一个函数来生成新的 DataFrame 并更新数据源
def generate_new_data():
    global ds, data
    new_data = pd.DataFrame({"name": ["new1", "new2", "new3"], "age": [20, 21, 22]})

    data = pd.concat([data, new_data], ignore_index=True)

    ds.reload(data)


# 添加一个按钮
ui.button("生成新数据", on_click=generate_new_data)

ui.run()
